package com.dzxcqmxm.servlet.photo;

import com.dzxcqmxm.Dao.PhotoDao;
import com.dzxcqmxm.Dao.impl.PhotoDaoImpl;
import com.dzxcqmxm.JB.Photo;
import com.dzxcqmxm.Dao.impl.testDel.deleteFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Payboi
 * @create 2022/6/4-21:37
 * @FileName delPhotoServlet
 * @Description
 */
@WebServlet("/delPhotoServlet1")
public class delPhotoServlet1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * 先删除本地，再删除数据库的
         */
        PhotoDao photoDao = new PhotoDaoImpl();
        Photo photo = new Photo();
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();
        try {
            //获得表单数据
            photo.setPhotoName(new String(request.getParameter("PhotoName" ).getBytes("iso-8859-1"),"UTF-8"));

            /**
             * 把本机照片也删掉,得获取路径
             * 从数据库里获得
             *
             */
            System.out.println("测试这里的执行");
            System.out.println("查看一下现在的photo.getPhotoName()"+photo.getPhotoName());

            try {
                /**
                 * 这个写是说通过相册名来删除
                 * 但其实是通过相册url来删除
                 * 我要先删除本地的，再删除数据库的，不然数据库的删了我再数据库找不到数据来删除本地得了
                 */
                photoArrayList =photoDao.photoFindbyPhotoName(photo.getPhotoName());
                System.out.println("打印删除照片1页面的photoArrayList"+photoArrayList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(photoArrayList!=null&&photoArrayList.size()>0){
                for(Photo photo1:photoArrayList){
                    String dir = photo1.getBenDiPhotoUrl();
                    System.out.println("看看被删除的照片的本地路径："+photo1.getBenDiPhotoUrl());
                    deleteFile.deleteFile(dir);
                }
            }
            try {
                photoDao.delPhoto(photo.getPhotoName());
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        response.sendRedirect("/dzxc/admin/ShenHe.jsp");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
